import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;


public class ClassQueryPage {
	private TagNode tagNode;
	
	public ClassQueryPage(String date, String dept) throws MalformedURLException, IOException{
		CleanerProperties props = new CleanerProperties();
		 
		String url="http://classes.iastate.edu/soc.jsp?term=" + date + "&dept=" + dept;
		url=url.replaceAll(" ", "%20");
		System.out.println("url: " +url);
		// set some properties to non-default values
		props.setTranslateSpecialEntities(true);
		props.setTransResCharsToNCR(true);
		props.setOmitComments(true);
		
		
		
		tagNode = new HtmlCleaner(props).clean(
			    new URL(url)
		);
		

		new PrettyXmlSerializer(props).writeToFile(
			    tagNode, "testBanana.xml", "utf-8"
			);
	}
	public void getClasses()
	{
		ArrayList<String> name = new ArrayList<String>();
		ArrayList<Integer> courseStart = new ArrayList<Integer>();
		ArrayList<String> abrev = new ArrayList<String>();
		ArrayList<String> classAbrev = new ArrayList<String>();
		ArrayList<String> title = new ArrayList<String>();
		ArrayList<String> classTitle = new ArrayList<String>();
		ArrayList<String> section = new ArrayList<String>();
		ArrayList<String> classSection = new ArrayList<String>();
		ArrayList<String> test = new ArrayList<String>();
		ArrayList<Course> courses = new ArrayList<Course>();
		ArrayList<String> location= new ArrayList<String>();
		ArrayList<String> professor= new ArrayList<String>();
		ArrayList<String> time= new ArrayList<String>();
		
		TagNode node = findTagNodeForName(tagNode, "table", "rules", "none");
		TagNode tbody = getChildByNumber(node, 0);
		TagNode childTags[] = tbody.getChildTags();
		int i =0;
		System.out.println("Classes Start AT:");
		
		Course courseListing=null;
		for(TagNode a: childTags){
			System.out.println("Line: " + i);
			TagNode course;
			if(a.getText().toString().contains("COURSE:")){
				System.out.println("Course");
				//System.out.println("Course: " + i);
				courseStart.add(i);
				//get the abrevs
				TagNode child=getChildByNumber(a, 2);
				String c = byeWhite(child.getText().toString());
				courseListing=new Course(c);
				courses.add(courseListing);
				abrev.add(child.getText().toString());
				
				}
			//get the titles
			if(a.getText().toString().contains("TITLE:")){
				System.out.println("Title");
				TagNode childName= getChildByNumber(a, 1);
				title.add(childName.getText().toString());
				String b = byeWhite(childName.getText().toString());
				courseListing.setTitle(b);
			}
			//get sections
			TagNode aba;
			if(findTagNodeForName(a, "td", "width", "25")!=null){
				System.out.println("Section");
				aba = getChildByNumber(a, 2);
				
				
				
	
				String ac = ridWhite(aba.getText().toString());
	
				if(aba.getText().toString()!=null){
					courseListing.addSection(ac);
				}
				else{
					courseListing.addSection("");
				}
				if(getChildByNumber(a,4)!=null){
					TagNode times= getChildByNumber(a,4);
					String tm=ridWhite(times.getText().toString());
					courseListing.addTime(tm);
				}
				if(getChildByNumber(a,5)!=null)
				{
					TagNode locations = getChildByNumber(a,5);
					String loc = ridWhite(locations.getText().toString());
					courseListing.addLocation(loc);
				}
				/*if(getChildByNumber(a,6)!=null)
				{
					TagNode prof =getChildByNumber(a,6);
					String pf=ridWhite(prof.getText().toString());
					courseListing.addProfessor(pf);
				}*/
					
				
			//		System.out.println("huh? " + ac);
					
				
				section.add(aba.getText().toString());
			//	time.add(times.getText().toString());
				//location.add(locations.getText().toString());
			//	professor.add(prof.getText().toString());
			}
			
			i++;
		}
	
		for(Course a :courses){
			System.out.println("name " +a.getAbrev());
			for(String s : a.getSections()){
				System.out.println("sections:  " + s);
			}
			for(String s : a.getLocations()){
				System.out.println("locations "  + s);
			}
			for(String s : a.getProfessors()){
				System.out.println("profs " + s);
			}
			for(String s : a.getTimes()){
				System.out.println("Times " +s);
			}
		}
		for(String a : section){ 
			String letters = "";
			for(int b =0; b<a.length(); b++){
				
				if((int)a.charAt(b)!=32 && (int)a.charAt(b)!=13 &&(int)a.charAt(b)!=10 )
				{	
					letters = letters + a.charAt(b);

				}
				
			}
			letters = letters.replace("&nbsp;", "");
			classSection.add(letters);
			
		}
		for(String a : classSection){
			System.out.println("Section: " + a);
		}
		//get the titles for the classes
		for(String a : title){
			String letters = "";
			for(int b =0; b<a.length(); b++){
				
				if((int)a.charAt(b)!=32 && (int)a.charAt(b)!=13 &&(int)a.charAt(b)!=10 )
				{	
					letters = letters + a.charAt(b);

				}
				
			}
			classTitle.add(letters);
			
		}
		for(String a : classTitle){
			System.out.println("Titles: " + a);
		}
	//get the abbreviations for the classes
		for(String a : abrev){
			String letters = "";
			for(int b =0; b<a.length(); b++){
				
				if((int)a.charAt(b)!=32 && (int)a.charAt(b)!=13 &&(int)a.charAt(b)!=10 )
				{	
					letters = letters + a.charAt(b);

				}
				
			}
			classAbrev.add(letters);
			
		}
		for(String a : classAbrev){
			System.out.println("Abrevs: " + a);
		}
		System.out.println();
		System.out.println("Classes From:");
		for(int j = 0;j<courseStart.size();j++){
			int start = courseStart.get(j);
			int end;
			if ( (j+1) < courseStart.size()){
				end = courseStart.get(j+1)-1;
			}
			else{
				end = childTags.length-1;
			}
			
			System.out.println(j + ": " + start + " - " + end);
		}
		
	}
	
	public void createClassFromTagArray(TagNode[] array, int start, int end){
		String course;
		String title;
		String preReq;
		String note;
		
		int sectionPosition;
		for(int i =start;i<=end;i++){
			if(array[i].getText().toString().contains("SECTION:")){
				sectionPosition = i;
			}
		}
		
		
		
		
	}
	public ArrayList<ClassModel> getClassData()
	{
		return null;
	}
	public static TagNode findTagNodeForName(TagNode node,String name,String attribute_name, String attribute_value){
			if(node.getName() != null && node.getName().equals(name)){
			//	System.out.println("Looking into attributes for :" + node.getName());
				Map<String,String> attributes = node.getAttributes();
				Set<String> keySet = attributes.keySet();
	
				for(String key: keySet){
					if(key.equals(attribute_name) && attributes.get(key).equals(attribute_value)){
						return node;
					}
				}
			}

			TagNode[] tags = node.getChildTags();
			for(TagNode tag:tags){
				TagNode newTag = findTagNodeForName(tag,name,attribute_name, attribute_value);
				if(newTag != null){
					return newTag;
				}
			}
		return null;

	}
	private String ridWhite(String a){
		String letters = "";
		for(int b =0; b<a.length(); b++){
			
			if((int)a.charAt(b)!=32 && (int)a.charAt(b)!=13 &&(int)a.charAt(b)!=10 )
			{	
				letters = letters + a.charAt(b);

			}
			
		}
		letters = letters.replace("&nbsp;", "");
		return letters;
		
	}
	private String byeWhite(String a){
		
			String letters = "";
			for(int b =0; b<a.length(); b++){
				
				if((int)a.charAt(b)!=32 && (int)a.charAt(b)!=13 &&(int)a.charAt(b)!=10 )
				{	
					letters = letters + a.charAt(b);

				}
				
			}
			return letters;
	}
	public static TagNode getChildByNumber(TagNode parent,int number){
		TagNode[] childTags = parent.getChildTags();
		return childTags[number];

	}
}
